package com.gexin.weixin.controller;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisKeyConst;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.weixin.controller.base.WxBaseController;
import com.gexin.weixin.interceptor.WexinAuthBaseInterceptor;
import com.gexin.weixin.interceptor.WexinAuthInterceptor;
import com.gexin.weixin.kit.WeXinCacheKeyConst;
import com.gexin.weixin.kit.WxAppConst;
import com.gexin.weixin.kit.WxConfigKit;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.*;

@ControllerBind(controllerKey = "/weixin/dev", viewPath = "WEB-INF/extend/weixin/")
public class WexinDevController extends WxBaseController {
	private static final Log log = Log.getLog(WexinDevController.class);

	public void index() {
		setAttr("user_id", 2);
		render("dev.html");
	}


	//获取token
	public  void getToken(){
		ApiConfig config = WxConfigKit.getApiConfig(1);
		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		renderJson(AccessTokenApi.getAccessToken());
	}

	//刷新token
	public  void refreshToken(){
		ApiConfig config = WxConfigKit.getApiConfig(1);
		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		CacheUtils.del("jfinal-weixin:token:".concat(config.getAppId()));  //删除token
		renderJson(AccessTokenApi.getAccessToken());
	}

	@Before(WexinAuthInterceptor.class)
	public void info() {
		setAttr("data", JsonKit.toJson(getLoginUser()));
		render("dev.html");
	}

	@Before(WexinAuthBaseInterceptor.class)
	public void base() {
		setAttr("data", getLoginUserOpenid());
		render("dev.html");
	}



	public void auth_success() {

		render("dev.html");
	}

	public void clear_cookie() {
		log.info("clear_cookie");
		removeCookie(WxAppConst.Weixin_UserCookie_Id);
		removeCookie(WxAppConst.Current_Weixin_Cookie_AppId);
		render("dev.html");
	}

	public void set_cookie() {
		WeixinUser user = WeixinUser.dao.findById("109656");
		if (user == null) {
			fail("自动回填的用户Id不存在！");
			return;
		}
		setCookie(WxAppConst.Weixin_UserCookie_Id, "109656", 6666666);
		setCookie(WxAppConst.Current_Weixin_Cookie_AppId, "1", 6666666);
		String uuid = StringUtil.generID();
		CacheUtils.set(WeXinCacheKeyConst.wx_token + uuid, user, RedisKeyConst.day * 30);//缓存三十天失效
		setAttr("UserToken",uuid);
		render("dev.html");
	}

	public void cookie() {
		render("dev.html");
	}


	public void genQrcode() {
		ApiConfig config = WxConfigKit.getApiConfig(1);
		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		ApiResult result = QrcodeApi.createPermanent("game:" + 1);
		String url = QrcodeApi.getShowQrcodeUrl(result.getStr("ticket"));
		renderText(url);
	}

	@Clear
	public void sendTemplate() {
		ApiConfig config = WxConfigKit.getApiConfig(1);
		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		TemplateData d = TemplateData.New();
		d.setTemplate_id("-hWKbgLBHa7efA1qsy_jgNXBghjHp1cXUASJYMMm5wU");
		d.setTouser("oikXNwGHVYhyXuJAGExqF_QXspLE");
		d.setUrl("http://www.baidu.com");
		d.add("first", "恭喜您获得XX奖");
		d.add("keyword1", "招商会");
		d.add("keyword2", "一等奖");
		d.add("keyword3", "2014年7月21日 18:36");
		d.add("remark", "请尽快到主席台领取相关奖品");
		TemplateMsgApi.send(d.build());
		renderText("success");
	}
}
